package utils;

import play.Logger;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author yangzhi
 * @date 2018/6/4
 */
public class DateUtils {

	private static final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/** mysql 存在时间戳溢出，定义默认最大上限时间2038-01-01 00:00:00 的 时间戳值 */
	public static final Long maxTimeMillis = Long.valueOf("2145888000000");

	public static String formatDateToString(Date date, String patten) {
		if (null == date) {
			return "";
		}
		SimpleDateFormat sd = new SimpleDateFormat(patten);
		return sd.format(date);
	}

	public static String getPostTimeList(String time){
		String result= "";
		if (!Utils.isNullOrEmpty(time)) {
			String ymd = time.substring(0,10);
			if (!ymd.equals(formatDateToString(new Date(),"yyyy-MM-dd"))){
				result = ymd;
			}else{
				String hm = time.substring(11,16);
				result =hm;
			}
		}
		return  result;

	}

	/**
	 * 时间字符串转date
	 * @param dateStr
	 * @return
	 */
	public static Date formatStrToDate(String dateStr,String pattern) {
		if (null == dateStr) {
			return null;
		}
		//SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sd = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = sd.parse(dateStr);
		} catch (ParseException e) {
			try {
				date=  new Date(Long.parseLong(dateStr));
			}catch (Exception ex){
				Logger.error("formatStrToDate:"+ex.getMessage());
			}
		}
		return date;
	}

	/**
	 * 获取当前时间戳
	 * @return
	 */
	public static long nowTimes() {
		return System.currentTimeMillis();
	}


	/**
	 * 获取两个日期之间的具体日期
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static List<String> getDays(String startTime, String endTime) {

		// 返回的日期集合
		List<String> days = new ArrayList<String>();

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date start = dateFormat.parse(startTime);
			Date end = dateFormat.parse(endTime);

			Calendar tempStart = Calendar.getInstance();
			tempStart.setTime(start);

			Calendar tempEnd = Calendar.getInstance();
			tempEnd.setTime(end);
			tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
			while (tempStart.before(tempEnd)) {
				days.add(dateFormat.format(tempStart.getTime()));
				tempStart.add(Calendar.DAY_OF_YEAR, 1);
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return days;
	}

	// 获得某天最大时间 2019-10-15 23:59:59

	/**
	 *
	 * @param date
	 * @return
	 */
	public static Date getEndOfDay(Date date) {
		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
		LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
		return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
	}

	// 获得某天最小时间 2019-10-15 00:00:00

	/**
	 *
	 * @param date
	 * @return
	 */
	public static Date getStartOfDay(Date date) {
		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
		LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
		return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
	}

	/**
	 * 获得某月最后一天
	 * @param dataDate
	 * @return
	 */
	public  static String getLastDay(String dataDate){
		Date date = null;
		String day_last = null;
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			date = format.parse(dataDate);
			//创建日历
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, 1);//加一个月
			calendar.set(Calendar.DATE, 1);//设置为该月第一天
			calendar.add(Calendar.DATE, -1);//再减一天即为上个月最后一天
			day_last = format.format(calendar.getTime());
			return day_last;
		}catch (Exception ex){
			return null;
		}
	}

	/**
	 * 获取某月第一天
	 * @param dataDate
	 * @return
	 */
	public static String getFirstDay(String dataDate){
		Date date = null;
		String day_first = null;
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			date = format.parse(dataDate);

			//创建日历
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			day_first = format.format(calendar.getTime());
			return day_first;
		}catch (Exception ex){
			return null;
		}
	}


	public static void main(String[] args){

		System.out.println(DateUtils.formatStrToDate(getFirstDay("2019-12-01"),"yyyy-MM-dd"));
		System.out.println(DateUtils.formatStrToDate(getLastDay("2019-12-01"),"yyyy-MM-dd"));

//		System.out.println(DateUtils.formatDateToString(DateUtils.getStartOfDay(DateUtils.formatStrToDate(getFirstDay("2019-12"),"yyyy-MM")),"yyyy-MM-dd HH:mm:ss"));
//
//		System.out.println(DateUtils.formatDateToString(DateUtils.getEndOfDay(DateUtils.formatStrToDate(getLastDay("2019-12"),"yyyy-MM")),"yyyy-MM-dd HH:mm:ss"));


	}


}
